from datetime import datetime

from django.db import models

# Create your models here.
from django.utils import timezone
from Administrative_office_system.utils.BaseModel import BaseModel


class topic_meeting(BaseModel):
    STATE = (
        ('voting', '投票中'),
        ('adopt', '同意'),
        ('suspension', '暂缓'),
        ('nopass', '未通过'),
        ('notstart', '未开始'),
        ('ending', '结束'),
    )
    TYPE = {
        ("yb", '院办'),
        ("db", '党办'),
        ("xz", '行政'),
    }
    STATUS = (
        ('progress', '进行中'),
        ('ending', '完结'),
    )
    topic = models.ForeignKey('administrative.Topics', verbose_name="议题", blank=True, null=True, on_delete=models.SET_NULL)
    state = models.CharField('状态', max_length=10, choices=STATE, default='notstart')
    meet_number = models.CharField('对应会议次数', max_length=10)
    topic_type = models.CharField('议题类型', max_length=10, choices=TYPE, default='yb')
    meet_time = models.DateTimeField(verbose_name='会议时间', default=timezone.now)
    MeetingStatus = models.CharField('会议进程', max_length=10, choices=STATUS, default='progress')
    topic_year = models.CharField('会议年份', max_length=20, default='2022')
    compere = models.ForeignKey('personal_center.User', verbose_name="主持人", blank=False, null=True, on_delete=models.CASCADE, default="")


class topic_bunber(BaseModel):
    STATE = (
        ('voting', '投票中'),
        ('agree', '同意'),
        ('opposition', '反对'),
        ('waiver', '弃权'),
    )
    leaders = models.ForeignKey(
        'personal_center.User',
        verbose_name="参会领导",
        blank=False,
        null=False,
        on_delete=models.CASCADE)
    topics_id = models.ForeignKey(
        'topic_meeting',
        verbose_name="议题投票",
        blank=False,
        null=False,
        on_delete=models.CASCADE)
    state = models.CharField('状态', max_length=10, choices=STATE, default='voting')
    node = models.TextField("意见", default='')

# 参会领导
class VoteLeader(BaseModel):
    STATE = (
        ('voting', '未投票'),
        ('agree', '同意'),
        ('opposition', '反对'),
        ('waiver', '弃权'),
    )
    leaders = models.ForeignKey(
        'personal_center.User',
        verbose_name="参会领导",
        blank=False,
        null=False,
        on_delete=models.CASCADE)
    topics_id = models.ForeignKey(
        'topic_meeting',
        verbose_name="投票议题",
        blank=False,
        null=False,
        on_delete=models.CASCADE)
    meet_number = models.CharField('会议编号（第几次会议）', max_length=20, default='')
    topic_year = models.CharField('会议年份', max_length=20, default='2022')
    node = models.TextField("意见", default='')
    state = models.CharField('状态', max_length=10, choices=STATE, default='voting')
    vote = models.CharField('是否投票', max_length=10, default=True)
    Participation = models.CharField('是否参会', max_length=10, default=True)
    host = models.CharField('是否主持', max_length=10, default=True)
    reason = models.TextField("不参会原因", default='')
    departure_reason = models.TextField("中途离会原因", default='', null=True)

class topic_sing(BaseModel):
    TYPE = {
        ("yb", '院办'),
        ("db", '党办'),
        ("xz", '行政'),
    }
    leaders = models.ForeignKey(
        'personal_center.User',
        verbose_name="参会领导",
        blank=False,
        null=False,
        on_delete=models.CASCADE)
    sing_time = models.CharField('签到时间', max_length=100, default=True)
    meet_number = models.CharField('对应会议次数', max_length=10)
    topic_type = models.CharField('议题类型', max_length=10, choices=TYPE, default='yb')
    topic_year = models.CharField('会议年份', max_length=20, default='')
    reason = models.TextField("不参会原因", default='')
    sing_type = models.CharField('签到类型', max_length=20, default='1')            # 1：扫码签到，2：请假
    uid = models.CharField(max_length=128, verbose_name='扫码设备', default='')

# 投票主表
class VotingMasterTable(BaseModel):
    TYPE = {
        ("radio", '单选'),
        ("multiple", '多选'),
    }
    STATE = (
        ('voting', '投票中'),
        ('ending', '结束'),
    )
    VIEWTYPE = (
        ("one", "投票者可见"),
        ("two", "发起者可见")
    )
    state = models.CharField('状态', max_length=10, choices=STATE, default='voting')
    apply_user = models.ForeignKey('personal_center.User', verbose_name="创建人", blank=True, null=True, on_delete=models.SET_NULL)
    vote_title = models.CharField(max_length=255, verbose_name="投票标题", null=True, default="")
    vote_content = models.TextField(verbose_name="详细内容", null=True, default="")
    vote_type = models.CharField('投票类型', max_length=10, choices=TYPE, default='radio')
    vote_deadline = models.CharField('投票截止时间', max_length=50, default='')
    vote_viewable = models.CharField('投票结果可查看类型', max_length=10, choices=VIEWTYPE, default='radio')

# 投票选项
class VotingOptionTable(BaseModel):
    voting_id = models.ForeignKey('VotingMasterTable', verbose_name="投票", blank=False, null=False, on_delete=models.CASCADE)
    option = models.CharField(max_length=255, verbose_name="投票选项", null=True, default="")

# 投票人
class VotingPeopleTable(BaseModel):
    voting_id = models.ForeignKey('VotingMasterTable', verbose_name="投票", blank=True, null=False, on_delete=models.CASCADE)
    apply_user = models.ForeignKey('personal_center.User', verbose_name="投票人", blank=True, null=True, on_delete=models.SET_NULL)
    voting_option = models.ManyToManyField('VotingOptionTable', verbose_name="投票选项", related_name='VotingOption', blank=True)

# 调研问卷
class QuestionnaireTable(BaseModel):
    STATE = (
        ('voting', '调研中'),
        ('ending', '结束'),
    )
    apply_user = models.ForeignKey('personal_center.User', verbose_name="发起人", blank=True, null=True, on_delete=models.SET_NULL)
    title = models.CharField(max_length=255, verbose_name="问卷标题", null=True, default="")
    illustrate = models.TextField(verbose_name="调研说明", null=True, default="")
    start_time = models.CharField('调研开始时间', max_length=50, default='')
    end_time = models.CharField('调研截止时间', max_length=50, default='')
    state = models.CharField('状态', max_length=10, choices=STATE, default='voting')

    def get_state_str(self):
        if self.state == 'voting':
            return '调研中'
        else:
            return '结束'

# 问卷小标题
class QuestionnaireTitle(BaseModel):
    TYPE = {
        ("a", '单选'),
        ("b", '多选'),
        ("c", '文本'),
    }
    survey_id = models.ForeignKey('QuestionnaireTable', verbose_name="调研问卷", blank=True, null=False, on_delete=models.CASCADE)
    survey_title = models.CharField(max_length=255, verbose_name="问卷标题", null=True, default="")
    option_type = models.CharField('类型', max_length=10, choices=TYPE, default='a')

# 问卷选项
class QuestionnaireQuestions(BaseModel):
    survey_id = models.ForeignKey('QuestionnaireTitle', verbose_name="问卷小标题", blank=True, null=False, on_delete=models.CASCADE)
    option = models.CharField(max_length=255, verbose_name="标题选项", null=True, default="")

# 问卷调研人员
class QuestionnairePeople(BaseModel):
    parent_id = models.ForeignKey('QuestionnaireTable', verbose_name="调研问卷", blank=True, null=True, on_delete=models.CASCADE)
    survey_id = models.ForeignKey('QuestionnaireTitle', verbose_name="问卷小标题", blank=True, null=False, on_delete=models.CASCADE)
    apply_user = models.ForeignKey('personal_center.User', verbose_name="投票人", blank=True, null=True, on_delete=models.SET_NULL)

class QuestionnaireRecord(BaseModel):
    parent_id = models.ForeignKey('QuestionnaireTitle', verbose_name="问卷标题", blank=True, null=True, on_delete=models.CASCADE)
    apply_user = models.ForeignKey('personal_center.User', verbose_name="投票人", blank=True, null=True, on_delete=models.SET_NULL)
    illustrate = models.TextField(verbose_name="所选选项", null=True, default="")

class ProcessNodesMainTable(BaseModel):
    process_name = models.CharField('流程名称', max_length=225, default='')
    process_coding = models.CharField('流程编码', max_length=50, default='')            # JSSRMYY-YT-YBYT-20240110
    operator = models.ForeignKey('personal_center.User', verbose_name="操作人", blank=True, null=True, on_delete=models.SET_NULL)
    abbreviation = models.CharField('简称', max_length=50, default='')
    module = models.ForeignKey('system_setup.CustomerFormModule', verbose_name="所属模块", blank=False, null=False, on_delete=models.CASCADE)


class ProcessNodesSecondaryTable(BaseModel):
    parent_id = models.ForeignKey('ProcessNodesMainTable', verbose_name="流程节点主表", blank=False, null=False, on_delete=models.CASCADE)
    serial = models.CharField('序号', max_length=50, default='')
    nodes = models.CharField('节点', max_length=50, default='')
    describe = models.TextField(verbose_name="描述", default='')

class CommitteeTable(BaseModel):
    TYPE = (
        ('1', '一级委员会'),
        ('2', '二级委员会'),
        ('3', '三级委员会'),
        ('4', '四级委员会'),
        ('5', '五级委员会'),
    )
    committee_name = models.CharField('委员会名称', max_length=255, default='')
    committee_level = models.CharField('委员会级别', max_length=10, choices=TYPE, default='1')
    chairman = models.ManyToManyField('personal_center.User', verbose_name='主任委员', related_name='chairman', blank=True)
    vice_chairman = models.ManyToManyField('personal_center.User', verbose_name='副主任委员', related_name='vice_chairman', blank=True)
    liaison_officer = models.ForeignKey('personal_center.User', verbose_name='联络员', related_name='liaison_officer', null=True, on_delete=models.CASCADE)
    company = models.ForeignKey('administrative.Company', verbose_name="办公室", blank=True, null=True, on_delete=models.CASCADE)

class CommitteeTablePeople(BaseModel):
    parent_id = models.ForeignKey('CommitteeTable', verbose_name="委员会主表", blank=False, null=False, on_delete=models.CASCADE)
    member = models.ForeignKey('personal_center.User', verbose_name='委员', null=True, on_delete=models.CASCADE)